package com.ezandroid.util;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Point;
import android.view.WindowManager;

/**
 * 显示工具类
 *
 * @author markmjw
 * @date 2015-07-05
 */
public class DisplayUtil {

    private static Point point;

    /**
     * dp转 px.
     *
     * @param value the value
     * @return the int
     */
    public static int dp2px(float value) {
        final float scale = Resources.getSystem().getDisplayMetrics().density;
        return (int) (value * scale + 0.5f);
    }

    /**
     * px转dp.
     *
     * @param value the value
     * @return the int
     */
    public static int px2dp(float value) {
        final float scale = Resources.getSystem().getDisplayMetrics().density;
        return (int) (value / scale + 0.5f);
    }

    /**
     * sp转px.
     *
     * @param value the value
     * @return the int
     */
    public static int sp2px(float value) {
        final float scale = Resources.getSystem().getDisplayMetrics().scaledDensity;
        return (int) (value * scale + 0.5f);
    }

    /**
     * px转sp.
     *
     * @param value the value
     * @return the int
     */
    public static int px2sp(float value) {
        final float scale = Resources.getSystem().getDisplayMetrics().scaledDensity;
        return (int) (value / scale + 0.5f);
    }

    /**
     * 获取屏幕尺寸
     *
     * @param context
     * @return
     */
    public static Point getDisplaySize(Context context) {
        if (null == point) {
            point = new Point();
            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            wm.getDefaultDisplay().getSize(point);
        }
        return point;
    }
}